# load data
file = 'ppg2008.csv'
data_1 = read.delim(file, header = TRUE, sep = ',')
# Print data
head(data_1)
# Margins area
par(oma=c(5,3,3,3)) # all sides have 3 lines of space
par(mar=c(5,4,4,2) + 0.1)

# load library
library("RColorBrewer")

row.names(data_1) = data_1$Name

# set lables
colnames(data_1)[2:21] = c('Games', 'Minutes', 'Points', 'Field Goals Made', 'Field Goal Attempts', 'Field Goal Percentage', 'Free Throws Made', 'Free Throw Attempts', 'Free Throw Percentage', 'Three-pointers made', 'Three-point Attempts', 'Three-point Percentage', 'Offensive Rebounds', 'Defensive Rebounds', 'Total Rebounds', 'Assists', 'Steals', 'Blocks', 'Turnovers', 'Personal Fouls')

# convert to matrix
data_1_matrix = data.matrix(data_1[,2:21])

heatmap(data_1_matrix, scale = "column", Colv = NA, Rowv = NA, main = "NBA Player Per Game Stats", col = brewer.pal(9,"Blues"))

# load data
file_2 = 'costcos-geocoded.csv'
data_2 = read.delim(file_2, header = TRUE, sep = ',')
# Print data
head(data_2)
str(data_2)
## 'data.frame':    417 obs. of  6 variables:
##  $ Address  : Factor w/ 416 levels "1 Industrial Lane",..: 49 255 387 316 243 276 269 268 341 124 ...
##  $ City     : Factor w/ 369 levels "Albany","Albuquerque",..: 139 137 210 147 7 7 256 330 330 117 ...
##  $ State    : Factor w/ 40 levels "Alabama","Alaska",..: 1 1 1 2 2 2 3 3 3 3 ...
##  $ Zip.Code : Factor w/ 415 levels "01089-4672","01923-1014",..: 115 114 116 415 414 413 239 238 237 235 ...
##  $ Latitude : num  34.7 33.4 32.4 58.4 61.1 ...
##  $ Longitude: num  -86.6 -86.8 -86.2 -134.5 -149.9 ...
# Load library for plotting data
library(ggplot2)
library(mapproj)
library(maps)
library(dplyr)

# Get the USA map
US = map_data("world") %>% filter(region == "USA")

# Create Plot and custom features
ggplot() +
    geom_polygon(data = US, aes(x = long, y = lat, group = group), fill="grey", alpha=0.3) +
    geom_point(data = data_2, aes(x = Longitude, y = Latitude), color = 'blue') +
    theme_void() + 
    xlim(-170, -55) +
    ylim(15, 72) +
    coord_map() +
    ggtitle("Costco Locations In the United States") +
    labs(caption = "Source: Data Collected By Nathan Yau") +
    theme(plot.title = element_text(face = "bold", size = 18),
          plot.caption = element_text(color = "light gray")
          )

# import Library
library(tidyverse)

data("volcano")

data_3 = as.data.frame(volcano) %>%
            rownames_to_column() %>%
            gather(key, value, -rowname) %>%
            mutate(key = as.numeric(gsub("V", "", key)), rowname = as.numeric(rowname))
head(data_3)
# import Library
library(directlabels)

ggplot(data_3) + 
      geom_contour_filled(aes(x = rowname,
                   y = key,
                   z = value,
                   colour = ..level..)) +
      scale_colour_brewer(palette = 'Blues') +
      scale_fill_brewer(palette = "Blues", direction = -1) +
      ggtitle("Valcano Elevation Plot") +
      labs(caption = "Source: Data is loaded from R volcano dataset",
          x = "",
          y = "") +
      theme_classic() +
      theme(plot.title = element_text(face = "bold", size = 18),
            plot.subtitle = element_text(color = "light gray"),
            plot.caption = element_text(color = "light gray")
            ) +
      guides(x = "none", y = "none")